// 管理系统API服务文件
import axios from 'axios';

const API_BASE_URL = 'http://localhost:5000/api';

// 游记项目接口
export interface TravelItem {
    id: string;
    title: string;
    content: string;
    location?: string;
    images: string[];
    video?: string | null;
    createdAt: string;
    updatedAt: string;
    status: 'pending' | 'approved' | 'rejected';
    rejectReason?: string | null;
    isDeleted: boolean;
    author: {
        _id: string;
        nickname: string;
        avatar: string;
    };
    submitTime?: string;
}

// 游记列表响应接口
interface TravelListResponse {
    success: boolean;
    count: number;
    total: number;
    totalPages: number;
    currentPage: number;
    data: TravelItem[];
}

// 审核操作响应接口
interface AuditActionResponse {
    success: boolean;
    message: string;
}

// 仪表盘统计接口
export interface DashboardStats {
    statistics: {
        totalUsers: number;
        totalTravels: number;
        pendingReviews: number;
        approvedTravels: number;
    };
    recentTravels: {
        id: string;
        title: string;
        author: string;
        submitTime: string;
        status: string;
    }[];
}

// 仪表盘响应接口
interface DashboardResponse {
    success: boolean;
    statistics: {
        totalUsers: number;
        totalTravels: number;
        pendingReviews: number;
        approvedTravels: number;
    };
    recentTravels: {
        id: string;
        title: string;
        author: string;
        submitTime: string;
        status: string;
    }[];
}

/**
 * 获取所有游记列表，包括不同状态的游记
 */
export const getAllTravels = async (
    page: number = 1,
    limit: number = 10,
    status?: string,
    deleted?: boolean
): Promise<TravelListResponse> => {
    try {
        const params = new URLSearchParams();
        params.append('page', page.toString());
        params.append('limit', limit.toString());

        // 如果指定了状态筛选
        if (status && status !== 'all') {
            params.append('status', status);
        }

        // 如果指定了删除状态筛选
        if (deleted !== undefined) {
            params.append('deleted', deleted.toString());
        }

        const response = await axios.get(`${API_BASE_URL}/admin/travels`, { params });
        return response.data;
    } catch (error) {
        console.error('获取所有游记列表失败:', error);
        throw error;
    }
};

/**
 * 搜索游记
 */
export const searchTravels = async (
    keyword: string,
    page: number = 1,
    limit: number = 10,
    deleted?: boolean
): Promise<TravelListResponse> => {
    try {
        const params = new URLSearchParams();
        params.append('page', page.toString());
        params.append('limit', limit.toString());
        params.append('search', keyword);

        // 如果指定了删除状态筛选
        if (deleted !== undefined) {
            params.append('deleted', deleted.toString());
        }

        const response = await axios.get(`${API_BASE_URL}/admin/travels`, { params });
        return response.data;
    } catch (error) {
        console.error('搜索游记失败:', error);
        throw error;
    }
};

/**
 * 审核通过游记
 */
export const approveTravel = async (id: string): Promise<AuditActionResponse> => {
    try {
        const response = await axios.put(`${API_BASE_URL}/admin/travels/${id}/approve`);
        return response.data;
    } catch (error) {
        console.error('审核通过游记失败:', error);
        throw error;
    }
};

/**
 * 拒绝游记
 */
export const rejectTravel = async (id: string, reason: string): Promise<AuditActionResponse> => {
    try {
        const response = await axios.put(`${API_BASE_URL}/admin/travels/${id}/reject`, { reason });
        return response.data;
    } catch (error) {
        console.error('拒绝游记失败:', error);
        throw error;
    }
};

/**
 * 删除游记
 */
export const deleteTravel = async (id: string): Promise<AuditActionResponse> => {
    try {
        const response = await axios.delete(`${API_BASE_URL}/admin/travels/${id}`);
        return response.data;
    } catch (error) {
        console.error('删除游记失败:', error);
        throw error;
    }
};

/**
 * 获取仪表盘统计数据
 */
export const getDashboardStats = async (): Promise<DashboardStats> => {
    try {
        const response = await axios.get<DashboardResponse>(`${API_BASE_URL}/admin/dashboard`);
        return {
            statistics: response.data.statistics,
            recentTravels: response.data.recentTravels
        };
    } catch (error) {
        console.error('获取仪表盘数据失败:', error);
        // 返回默认数据
        return {
            statistics: {
                totalUsers: 0,
                totalTravels: 0,
                pendingReviews: 0,
                approvedTravels: 0
            },
            recentTravels: []
        };
    }
};

// 导出所有API函数
export const api = {
    getAllTravels,
    searchTravels,
    approveTravel,
    rejectTravel,
    deleteTravel,
    getDashboardStats
};

export default api; 